package com.qigao.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qigao.mall.api.ItemCatService;
import com.qigao.mall.api.ShopInfoService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.DefultItemCatEnum;
import com.qigao.mall.commons.enums.IdTypeEnum;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.IDUtils;
import com.qigao.mall.jdbc.mapper.TbItemCatMapper;
import com.qigao.mall.jdbc.model.SysCompany;
import com.qigao.mall.jdbc.model.TbItemCat;
import com.qigao.mall.jdbc.model.TbShopInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author luiz
 * @Title: com.qigao.mall.service
 * @ProjectName three-mall
 * @date 2019-11-2019/11/1 14:11
 * @Description: 商品分类服务具体实现类
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    TbItemCatMapper tbItemCatMapper;
    @Resource
    private ShopInfoService shopInfoService;

    @Override
    public Result<List<ItemCatResultDto>> findItemCatLevelOne(String shopNo) {
        List<TbItemCat> tbItemCats=tbItemCatMapper.selectLevelOneByShopNo(shopNo);
        List<ItemCatResultDto> itemCatDtos=new ArrayList<>();
        if(tbItemCats!=null && tbItemCats.size()>=1){
            for(TbItemCat tbItemCat:tbItemCats){
                ItemCatResultDto itemCatDto=convertItemCatToDto(tbItemCat);
                itemCatDtos.add(itemCatDto);
            }
            return  Result.newSuccess(itemCatDtos);
        }else{
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }

    }

    @Override
    public void create(ItemCatCreateDto itemCatCreateDto) {
        ShopInfoDto shopInfoDto=shopInfoService.findByShopNo(itemCatCreateDto.getShopNo());
        TbItemCat tbItemCat=new TbItemCat();
        String catNo= IDUtils.genIdStr(IdTypeEnum.ITEM_CAT.getCode());
        tbItemCat.setCatNo(catNo);
        tbItemCat.setShopNo(itemCatCreateDto.getShopNo());
        tbItemCat.setMerNo(shopInfoDto.getMerNo());
        tbItemCat.setPcatNo(itemCatCreateDto.getPCatNo());
        tbItemCat.setCatName(itemCatCreateDto.getCatName());
        tbItemCat.setItemSort(itemCatCreateDto.getItemSort());
        tbItemCat.setItemLevel(1);
        if(!StringUtils.isEmpty(itemCatCreateDto.getPCatNo())){
            TbItemCat pTbItemCat=tbItemCatMapper.selectByCatNo(itemCatCreateDto.getPCatNo());
            tbItemCat.setItemLevel(pTbItemCat.getItemLevel()+1);
        }
        tbItemCat.setCreateNo(itemCatCreateDto.getUserNo());
        tbItemCat.setCreateTime(new Date());
        tbItemCat.setModiNo(itemCatCreateDto.getUserNo());
        tbItemCat.setModiTime(new Date());
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public void modi(ItemCatModiDto itemCatModiDto) {
        ShopInfoDto shopInfoDto=shopInfoService.findByShopNo(itemCatModiDto.getShopNo());
        TbItemCat tbItemCat=new TbItemCat();
        tbItemCat.setShopNo(itemCatModiDto.getShopNo());
        if(shopInfoDto!=null)
        tbItemCat.setMerNo(shopInfoDto.getMerNo());
        tbItemCat.setCatNo(itemCatModiDto.getItemCatNo());
        tbItemCat.setPcatNo(itemCatModiDto.getPCatNo());
        tbItemCat.setCatName(itemCatModiDto.getCatName());
        tbItemCat.setItemSort(itemCatModiDto.getItemSort());
        tbItemCat.setCreateNo(itemCatModiDto.getUserNo());
        tbItemCat.setCreateTime(new Date());
        tbItemCat.setModiNo(itemCatModiDto.getUserNo());
        tbItemCat.setModiTime(new Date());
        tbItemCatMapper.updateByItemCatNo(tbItemCat);
    }

    @Override
    public Result deleteByItemCatNo(String itemCatNo) {
        tbItemCatMapper.deleteByItemCatNo(itemCatNo);
        return Result.newSuccess();
    }

    @Override
    public ItemCatResultDto findByItemCatNo(String catNo) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByCatNo(catNo);
        return  convertItemCatToDto(tbItemCat);
    }

    @Override
    public Result<List<ItemCatResultDto>> findAll(String shopNo) {
        List<TbItemCat> tbItemCats=tbItemCatMapper.selectAllByShopNo(shopNo);
        List<ItemCatResultDto> itemCatResultDtos=new ArrayList<>();
        if(tbItemCats!=null && tbItemCats.size()>=1){
            for(TbItemCat tbItemCat:tbItemCats){
                ItemCatResultDto itemCatDto=convertItemCatToDto(tbItemCat);
                itemCatResultDtos.add(itemCatDto);
            }
            return Result.newSuccess(itemCatResultDtos);
        }else{
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
    }

    /**
     * 创建默认分类
     * @param tbShopInfo
     */
    @Override
    public void createDefult(TbShopInfo tbShopInfo) {
        int sortNum=1;
        for(DefultItemCatEnum defultItemCatEnum:DefultItemCatEnum.values()){
            TbItemCat tbItemCat=new TbItemCat();
            String catNo= IDUtils.genIdStr(IdTypeEnum.ITEM_CAT.getCode());
            tbItemCat.setCatNo(catNo);
            tbItemCat.setCatName(defultItemCatEnum.getName());
            tbItemCat.setItemSort(sortNum);
            tbItemCat.setItemLevel(1);
            tbItemCat.setShopNo(tbShopInfo.getShopNo());
            tbItemCat.setMerNo(tbShopInfo.getMerNo());
            tbItemCat.setCreateNo(tbShopInfo.getCreateNo());
            tbItemCat.setCreateTime(new Date());
            tbItemCat.setModiNo(tbShopInfo.getCreateNo());
            tbItemCat.setModiTime(new Date());
            tbItemCatMapper.insertSelective(tbItemCat);
            sortNum++;
        }
    }

    @Override
    public Page<ItemCatResultDto> listPage(ItemCatCondDto itemCatCondDto) {
        //使用PageHelper类进行分页
        PageHelper.startPage(itemCatCondDto.getPageNum(), itemCatCondDto.getPageSize());
        Page<TbItemCat> itemCatPage = (Page<TbItemCat>) tbItemCatMapper.selectList(itemCatCondDto);
        List<TbItemCat> itemCatList = itemCatPage.getResult();
        Page<ItemCatResultDto> itemCatResultDtoPage = new Page<>();
        BeanUtils.copyProperties(itemCatPage, itemCatResultDtoPage);
        List<ItemCatResultDto> itemCatResultDtos = buildDto(itemCatList);
        itemCatResultDtoPage.addAll(itemCatResultDtos);
        return itemCatResultDtoPage;
    }

    @Override
    public List<ItemCatResultDto> listTree() {
        List<TbItemCat> itemCatList=tbItemCatMapper.selectAll();
        List<ItemCatResultDto> itemCatResultDtos = buildDto(itemCatList);
        return buildTree(itemCatResultDtos);
    }

    private List<ItemCatResultDto> buildTree(List<ItemCatResultDto> all) {
        List<ItemCatResultDto> collect = all.stream().filter(m -> StringUtils.isEmpty(m.getPcatNo())).map(
                (m) -> {
                    m.setChildren(buildChild(m, all));
                    return m;
                }
        ).collect(Collectors.toList());
        return collect;
    }

    private  List<ItemCatResultDto>  buildChild(ItemCatResultDto root,List<ItemCatResultDto> all) {
        List<ItemCatResultDto> children = all.stream().filter(m -> {
            return Objects.equals(m.getPcatNo(), root.getCatNo());
        }).map(
                (m) -> {
                    m.setChildren(buildChild(m, all));
                    return m;
                }
        ).collect(Collectors.toList());
        return children;
    }

    private List<ItemCatResultDto> buildDto(List<TbItemCat> itemCatList) {
        List<ItemCatResultDto> itemCatResultDtos=new ArrayList<>();
        if(!CollectionUtils.isEmpty(itemCatList)){
            for(TbItemCat tbItemCat:itemCatList){
                ItemCatResultDto itemCatResultDto=new ItemCatResultDto();
                BeanUtils.copyProperties(tbItemCat,itemCatResultDto);
                itemCatResultDtos.add(itemCatResultDto);
            }
        }
        return itemCatResultDtos;
    }

    @Override
    public Result<List<ItemCatResultDto>> findChildItemCatByNo(String catNo) {
        List<TbItemCat> tbItemCats=tbItemCatMapper.selectChildByNo(catNo);
        List<ItemCatResultDto> itemCatResultDtos=new ArrayList<>();
        if(tbItemCats!=null && tbItemCats.size()>=1){
            for(TbItemCat tbItemCat:tbItemCats){
                ItemCatResultDto itemCatDto=convertItemCatToDto(tbItemCat);
                itemCatResultDtos.add(itemCatDto);
            }
            return Result.newSuccess(itemCatResultDtos);
        }else{
            return Result.newError(ResultCode.COMMON_DATA_NOT_EXISTS);
        }
    }

    private ItemCatResultDto convertItemCatToDto(TbItemCat tbItemCat) {
        if(tbItemCat==null)return null;
        ItemCatResultDto itemCatDto=new ItemCatResultDto();
        BeanUtils.copyProperties(tbItemCat,itemCatDto);
        return itemCatDto;
    }

}
